package com.xzz.window;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2022-02-11 09:16
 * 1984. 学生分数的最小差值
 **/
public class MinimumDifference {
    public static void main(String[] args) {
//        System.out.println(minimumDifference(new int[]{9, 4, 1, 7}, 3));
        System.out.println(minimumDifference(new int[]{87063, 61094, 44530, 21297, 95857, 93551, 9918}, 6));
    }

    public static int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);
        int res = nums[k-1]-nums[0];
        for (int i = k; i < nums.length; i++) {
            res = Math.min(res, nums[i] - nums[i - k + 1]);
        }
        return res;
    }
}
